<extend name="./Application/Admin/View/Layouts/application.html"/>

<block name="css">
    <link rel="stylesheet" href="__PUBLIC__/assets/admin/common.css">
</block>

<block name="content">
    <div class="admin-content">
        <div class="admin-content-body">
            <div class="am-cf am-padding am-padding-bottom-0">
                <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">广告列表</strong> /
                    <small>Advert List</small>
                </div>
            </div>

            <hr>

            <div class="am-g">
                <div class="am-u-sm-12 am-u-md-6">
                    <div class="am-btn-toolbar">
                        <div class="am-btn-group am-btn-group-xs">
                            <a href="{{:U('Advert/add')}}" class="am-btn am-btn-default"><span
                                    class="am-icon-plus"></span> 新增
                            </a>
                            <button type="button" class="delete_all am-btn am-btn-default"><span class="am-icon-trash"></span> 删除
                            </button>
                        </div>
                    </div>
                </div>

                <div class="am-u-sm-12 am-u-md-3">
                    <div class="am-input-group am-input-group-sm">
                        <input type="text" class="am-form-field">
                        <span class="am-input-group-btn">
            <button class="am-btn am-btn-default" type="button">搜索</button>
          </span>
                    </div>
                </div>
            </div>

            <div class="am-g">
                <div class="am-u-sm-12">
                    <form class="am-form">
                        <table class="am-table am-table-striped am-table-hover table-main">
                            <thead>
                            <tr>
                                <th>
                                    <input type="checkbox" class="check_all"/>
                                </th>
                                <th>编号</th>
                                <th>缩略图</th>
                                <th>标题</th>
                                <th>广告地址</th>
                                <th>描述信息</th>
                                <th>排序</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>

                            <volist name="adverts" id="advert">
                                <tr data-id="{{$advert.id}}">
                                    <td>
                                        <input type="checkbox" name="ids[]" value="{{$advert.id}}" class="checked_id"/>
                                    </td>
                                    <td>{{$advert.id}}</td>
                                    <td>
                                        <img class="am-circle" src="{{$advert.thumb}}" style="width: 50px;height:50px;" alt="">
                                    </td>
                                    <td>{{$advert.title}}</td>
                                    <td>
                                        <a href="{{$advert.url}}">{{$advert.url}}</a>
                                    </td>
                                    <td>{{$advert.description}}</td>
                                    <td>
                                        <input type="hidden" name="id[]" value="{{$advert.id}}">
                                        <input type="text" name="sort_order[]" class="sort_order" value="{{$advert.sort_order}}" style="width:100px;">
                                    </td>
                                    <td>
                                        <div class="am-btn-toolbar">
                                            <div class="am-btn-group am-btn-group-xs">
                                                <a href="{{:U('edit',['id'=>$advert[id]])}}"
                                                   class="am-btn am-btn-default am-btn-xs am-text-secondary"><span
                                                        class="am-icon-pencil-square-o"></span> 编辑
                                                </a>

                                                <button type="button"
                                                        class="delete_advert am-btn am-btn-default am-btn-xs am-text-danger am-hide-sm-only">
                                                    <span class="am-icon-trash-o"></span> 删除
                                                </button>
                                            </div>
                                        </div>
                                    </td>
                                </tr>
                            </volist>

                            </tbody>
                        </table>
                        <div class="am-cf">
                            共 {{$count}} 条记录
                            <div class="am-fr">
                                <ul class="am-pagination">
                                    {{$page}}
                                </ul>
                            </div>
                        </div>
                        <hr/>
                    </form>
                </div>

            </div>
        </div>

        <footer class="admin-content-footer">
            <hr>
            <p class="am-padding-left">© 2014 AllMobilize, Inc. Licensed under MIT license.</p>
        </footer>

        <!--删除模态框-->
        <div class="am-modal am-modal-confirm" tabindex="-1" id="deleteAdvert">
            <div class="am-modal-dialog">

                <input type="hidden" id="advert_id">
                <div class="am-modal-hd">删除广告</div>
                <div class="am-modal-bd">
                    你，确定要删除这条记录吗？
                </div>
                <div class="am-modal-footer">
                    <span class="am-modal-btn" data-am-modal-cancel>取消</span>
                    <span class="delete_confirm am-modal-btn" data-am-modal-confirm>确定</span>
                </div>
            </div>
        </div>

    </div>
</block>
<block name="js">
    <script>
        $(function () {
            //ajax删除单条
            $('#doc-modal-list').find('.am-icon-close').add('.delete_advert').on('click', function () {
                $('#deleteAdvert').modal();
                var id = $(this).parents('tr').data('id');
                $("#advert_id").val(id);
            })

            $(".delete_confirm").click(function () {
                var id = $("#advert_id").val();

                $.ajax({
                    type : 'post',
                    url : "{{:U('ajax_delete')}}",
                    data : {id:id},
                    success : function () {
                        $("tr[data-id = '"+id+"']").fadeOut(400);
                        $('#deleteAdvert').modal().hide();
                    }
                })
                return false;
            })

            //全选和全不选
            $(".check_all").click(function () {
                var checked = $(this).prop('checked');
                $('.checked_id').prop('checked',checked);
            })
            //下面全选中上面也选中
            $(".checked_id").click(function () {
                var allLength = $('.checked_id').length;
                var check_length = $(".checked_id:checked").length;
                if(allLength == check_length){
                    $(".check_all").prop('checked',true);
                }else{
                    $(".check_all").prop('checked',false);
                }
            })

            //ajax多选删除
            $(".delete_all").click(function () {
                var length = $(".checked_id:checked").length;
                if(length == 0){
                    alert('请你至少选择一条');return false;
                }

                $.post("{{:U('ajax_delete_all')}}",$(".checked_id:checked").serialize(),function () {
                    $(".checked_id:checked").each(function () {
                      $(this).parents('tr').fadeOut(400);
                    })
                })

                return false;
            })

            //ajax排序
            $('.sort_order').change(function () {
                var info = {
                    id : $(this).parents('tr').data('id'),
                    sort_order : $(this).val()
                }
                $.post("{{:U('ajax_sort_order')}}",info,function () {
                    window.location.reload();
                })

            })


        })
    </script>
</block>

